package com.boot.springBoot.config.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import lombok.extern.slf4j.Slf4j;

/**
 * 数据库连接池 的配置
    * @ClassName: DruidConfiguration  
    * @Description: TODO(这里用一句话描述这个类的作用)  
    * @author Administrator  
    * @date 2018年10月12日  
    *
 * 配好之后就可以访问 http://localhost:8888/druid/index.html
 */
@Configuration
public class DruidConfiguration {

	private final Logger log = LoggerFactory.getLogger(this.getClass());


	
	@Bean
	ServletRegistrationBean druidStatViewServle() {
		// TODO Auto-generated method stub
		log.info("---------加载数据库连接池---------------");
		//注册服务
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean
				(new StatViewServlet(),"/druid/*");
		// 白名单(为空表示,所有的都可以访问,多个IP的时候用逗号隔开)
//		servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
//		// IP黑名单 (存在共同时，deny优先于allow) 
//		servletRegistrationBean.addInitParameter("deny", "127.0.0.2");
//		// 设置登录的用户名和密码
		servletRegistrationBean.addInitParameter("loginUsername", "yang");
		servletRegistrationBean.addInitParameter("loginPassword", "123456");
//		// 是否能够重置数据.
//		servletRegistrationBean.addInitParameter("resetEnable", "false");
		return servletRegistrationBean;
	}
	
	
	@Bean
	FilterRegistrationBean druidStatFilter() {
		// TODO Auto-generated method stub
		FilterRegistrationBean filterRegistrationBean =  new FilterRegistrationBean(
				new WebStatFilter());
		// 添加过滤规则
		filterRegistrationBean.addUrlPatterns("/*");
		// 添加不需要忽略的格式信息
		filterRegistrationBean.addInitParameter("exclusions",
				"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		
		log.info("##########druid初始化成功!##############");
		return filterRegistrationBean;
	}
	
}
